Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Parsimmon is a library for writing simple, elegant parsers in JavaScript. It allows you to construct parsers for custom languages or data formats using a combinator-based approach.
Basic Parsing
This feature allows you to create a basic parser that matches a specific string. In this example, the parser is set to match the string 'hello'.
const P = require('parsimmon');
const parser = P.string('hello');
console.log(parser.parse('hello')); // { status: true, value: 'hello' }
console.log(parser.parse('world')); // { status: false, expected: ['hello'], index: { offset: 0, line: 1, column: 1 } }
Sequence Parsing
This feature allows you to create a parser that matches a sequence of patterns. In this example, the parser matches the sequence 'hello', whitespace, and 'world'.
const P = require('parsimmon');
const parser = P.seq(P.string('hello'), P.whitespace, P.string('world'));
console.log(parser.parse('hello world')); // { status: true, value: ['hello', ' ', 'world'] }
console.log(parser.parse('hello there')); // { status: false, expected: ['world'], index: { offset: 6, line: 1, column: 7 } }
Custom Parsers
This feature allows you to create custom parsers using regular expressions and combinators. In this example, the parser matches a sequence of digits.
const P = require('parsimmon');
const digit = P.regexp(/[0-9]/).desc('a digit');
const digits = digit.many().map(digits => digits.join(''));
console.log(digits.parse('12345')); // { status: true, value: '12345' }
console.log(digits.parse('abc')); // { status: false, expected: ['a digit'], index: { offset: 0, line: 1, column: 1 } }
Error Handling
This feature allows you to handle parsing errors gracefully by providing alternative parsing options. In this example, the parser matches either 'hello' or 'world'.
const P = require('parsimmon');
const parser = P.string('hello').or(P.string('world'));
console.log(parser.parse('hello')); // { status: true, value: 'hello' }
console.log(parser.parse('world')); // { status: true, value: 'world' }
console.log(parser.parse('foo')); // { status: false, expected: ['hello', 'world'], index: { offset: 0, line: 1, column: 1 } }
Nearley is a fast, feature-rich parser toolkit for JavaScript. It uses a different approach called Earley parsing, which can handle more complex grammars compared to Parsimmon. Nearley is more powerful but can be more complex to use.
PEG.js is a simple parser generator for JavaScript based on the Parsing Expression Grammar (PEG) formalism. It allows you to define your grammar in a concise syntax and generates a parser from it. PEG.js is more declarative compared to Parsimmon's combinator-based approach.
Jison is a parser generator that converts a context-free grammar into a JavaScript parser. It is similar to tools like Bison or Yacc but for JavaScript. Jison is more suitable for complex language parsing tasks compared to Parsimmon.
Authors: @jneen and @laughinghan
Maintainer: @wavebeem
Parsimmon is a small library for writing big parsers made up of lots of little parsers. The API is inspired by parsec and Promises/A+.
Parsimmon supports IE7 and newer browsers, along with Node.js. It can be used as a standard Node module through npm (named parsimmon
), or directly in the browser through a script tag, where it exports a global variable called Parsimmon
. To download the latest browser build, use the npmcdn version. For more information on how to use npmcdn, see the npmcdn homepage.
Full API documentation in API.md
.
See the examples directory for annotated examples of parsing JSON, Lisp, and math.
A Parsimmon parser is an object that represents an action on a stream of text, and the promise of either an object yielded by that action on success or a message in case of failure. For example, Parsimmon.string('foo')
yields the string 'foo'
if the beginning of the stream is 'foo'
, and otherwise fails.
The method .map
is used to transform the yielded value. For example,
Parsimmon.string('foo')
.map(function(x) { return x + 'bar'; })
will yield 'foobar'
if the stream starts with 'foo'
. The parser
Parsimmon.regexp(/[0-9]+/)
.map(function(x) { return Number(x) * 2; })
will yield the number 24
when it encounters the string '12'
.
Calling .parse(string)
on a parser parses the string and returns an object with a boolean status
flag, indicating whether the parse succeeded. If it succeeded, the value
attribute will contain the yielded value. Otherwise, the index
and expected
attributes will contain the index of the parse error (with offset
, line
and column
properties), and a sorted, unique array of messages indicating what was expected.
The error object can be passed along with the original source to Parsimmon.formatError(source, error)
to obtain a human-readable error string.
Thanks to @bd82 we have a good benchmark comparing Parsimmon CPU performance to several other parser libraries with a simple JSON parser example.
Parsimmon is also compatible with fantasyland. It is a Semigroup, an Applicative Functor, and a Monad.
0.9.2 (2016-08-07)
browser
field to package.json
so unpkg serves the correct file.README.md
.FAQs
A monadic LL(infinity) parser combinator library
The npm package parsimmon receives a total of 97,611 weekly downloads. As such, parsimmon popularity was classified as popular.
We found that parsimmon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.